package com.ict.txmsv2.controller.system;

import com.ict.txmsv2.model.MethodDescrition;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.Role;
import com.ict.txmsv2.model.User;
import com.ict.txmsv2.services.RoleService;
import com.ict.txmsv2.services.UserRoleService;
import com.ict.txmsv2.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-17
 */
@RestController
@RequestMapping("/system/oper")
public class SystemOperController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleService userRoleService;

    @DeleteMapping("/{id}")
    @MethodDescrition("删除user")
    public RespBean delUser(@PathVariable Integer id){
        if (userService.deleteDetail(id) == 1){
            return RespBean.ok("没了");
        }
        return RespBean.error("煜林的错，不是我");
    }

    @GetMapping("/grades")
    @MethodDescrition("获取所有年级")
    public List<String> getAllGrade(){
        return userService.getAllGrade();
    }

    @GetMapping("/")
    @MethodDescrition("条件获取所有用户")
    public List<User> getAllUserAndHisRoles(String name, String grade){
        return userService.getAllUserAndHisRoles(name,grade);
    }

    @GetMapping("/roles")
    @MethodDescrition("获取所有角色")
    public List<Role> getAllRoles(){
        return roleService.getAllRole();
    }

    @PutMapping("/")
    @MethodDescrition("是否禁用")
    public RespBean updateEnable(User user){
        if (userService.updateEnable(user) == 1){
            return RespBean.ok("禁用成功");
        }
        return RespBean.error("失败了");
    }

    @PutMapping("/roles/{userId}/{rids}")
    @MethodDescrition("修改user对应角色")
    public RespBean updateRolesByUserId(@PathVariable Integer userId, @PathVariable Integer[] rids){
        if (userRoleService.updateRolesByUserid(userId,rids)){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
